﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_SalaryPromoteCmdDtls : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private ICommandDetailBLL dtldBLL = new CommandDetailBLL();

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            txtOldSalary.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            txtNewSalary.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            txtBase.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            txtPercent.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            txtAmount.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "cmd_dtls_seq").ToString();

                    cmbEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "full_name").ToString(), DataBinder.Eval(DataItem, "emp_seq").ToString()));
                    cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "emp_seq").ToString();
                    lblPosRef.Text = DataBinder.Eval(DataItem, "new_pos_ref").ToString();
                    lblPosName.Text = DataBinder.Eval(DataItem, "new_pos_name").ToString();
                    lblClassName.Text = DataBinder.Eval(DataItem, "class_name").ToString();
                    lblJobSiteName.Text = DataBinder.Eval(DataItem, "new_job_site_name").ToString();
                    txtOldSalary.Text = DataBinder.Eval(DataItem, "old_salary", "{0:N2}").ToString();
                    txtNewSalary.Text = DataBinder.Eval(DataItem, "new_salary", "{0:N2}").ToString();

                    txtBase.Text = DataBinder.Eval(DataItem, "prm_base", "{0:N2}").ToString();
                    txtPercent.Text = DataBinder.Eval(DataItem, "prm_percent", "{0:N2}").ToString();
                    txtAmount.Text = DataBinder.Eval(DataItem, "prm_amount", "{0:N2}").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                    txtDate.SetValue(DataBinder.Eval(DataItem, "start_date"));
                }
                else
                {

                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbEmployee.Items.Clear();

            DataSet ds = empBLL.LoadEmployees((int?)null, Session["EMPTYPE"].ToString(), true, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["FULL_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(150);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["POS_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(100);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }
        private void GetEmployeeInfo()
        {
            DataSet ds = empBLL.LoadEmployees(Int32.Parse(cmbEmployee.SelectedValue), "", true, "");
            DataTable dt = null;

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                lblID_pos.Text = dt.Rows[0]["POS_SEQ"].ToString();
                lblPosRef.Text = dt.Rows[0]["POS_REF"].ToString();
                lblPosName.Text = dt.Rows[0]["POS_NAME"].ToString();
                lblID_class.Text = dt.Rows[0]["CLASS_SEQ"].ToString();
                lblClassName.Text = dt.Rows[0]["CLASS_NAME"].ToString();
                lblID_jobSite.Text = dt.Rows[0]["JOB_SITE_SEQ"].ToString();
                lblJobSiteName.Text = dt.Rows[0]["JOB_SITE_NAME"].ToString();
                txtOldSalary.Text = dt.Rows[0]["SALARY"].ToString();
            }
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                string id = Session["id"].ToString();
                string[] str = id.Split('|');

                dtldBLL.Insert(int.Parse(str[0])
                    , Int32.Parse(cmbEmployee.SelectedValue)
                    , DateTime.Parse(txtDate.GetStringValue())
                    , Int32.Parse(lblID_pos.Text.Trim())
                    , lblPosRef.Text.Trim()
                    , lblPosName.Text.Trim()
                    , Int32.Parse(lblID_class.Text.Trim())
                    , Int32.Parse(lblID_jobSite.Text.Trim())
                    , lblJobSiteName.Text.Trim()
                    , Double.Parse(txtNewSalary.Text)
                    , Double.Parse(txtOldSalary.Text)
                    , Double.Parse(txtBase.Text)
                    , Double.Parse(txtPercent.Text)
                    , Double.Parse(txtAmount.Text)
                    , txtRemarks.Text.Trim()
                    , ssMng.EmpSeqI);


                //cmdBLL.InsertSalaryCommandDetail(int.Parse(str[0])
                //    , Int32.Parse(cmbEmployee.SelectedValue)
                //    , DateTime.Parse(txtDate.GetStringValue())
                //    , int.Parse(lblID_pos.Text)
                //    , lblPosRef.Text
                //    , lblPosName.Text
                //    , int.Parse(lblID_class.Text)
                //    , int.Parse(lblID_jobSite.Text)
                //    , lblJobSiteName.Text
                //    , Double.Parse(txtNewSalary.Text)
                //    , Double.Parse(txtOldSalary.Text)
                //    , Double.Parse(txtBase.Text)
                //    , Double.Parse(txtPercent.Text)
                //    , Double.Parse(txtAmount.Text)
                //    , txtRemarks.Text.Trim()
                //    , ssMng.EmpSeq);
            }
            catch(Exception ex)
            {
                
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                dtldBLL.Update(Int32.Parse(lblID.Text.Trim())
                    , Int32.Parse(cmbEmployee.SelectedValue)
                    , DateTime.Parse(txtDate.GetStringValue())
                    , Int32.Parse(lblID_pos.Text.Trim())
                    , lblPosRef.Text.Trim()
                    , lblPosName.Text.Trim()
                    , Int32.Parse(lblID_class.Text.Trim())
                    , Int32.Parse(lblID_jobSite.Text.Trim())
                    , lblJobSiteName.Text.Trim()
                    , Double.Parse(txtNewSalary.Text)
                    , Double.Parse(txtOldSalary.Text)
                    , Double.Parse(txtBase.Text)
                    , Double.Parse(txtPercent.Text)
                    , Double.Parse(txtAmount.Text)
                    , txtRemarks.Text.Trim()
                    , ssMng.EmpSeqI);

                //cmdBLL.UpdateSalaryCommandDetail(int.Parse(lblID.Text)
                //    , DateTime.Parse(txtDate.GetStringValue())
                //    , Double.Parse(txtNewSalary.Text)
                //    , Double.Parse(txtOldSalary.Text)
                //    , Double.Parse(txtBase.Text)
                //    , Double.Parse(txtPercent.Text)
                //    , Double.Parse(txtAmount.Text)
                //    , txtRemarks.Text.Trim()
                //    , ssMng.EmpSeq);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbEmployee_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GetEmployeeInfo();
        }        
        

    }
}